package com.dap.cms.governance.controller;

import com.dap.cms.governance.po.CmsComponentInfoEnvPo;
import com.dap.cms.governance.po.CmsEnvPo;
import com.dap.cms.governance.service.CmsComponentInfoEnvService;
import com.dap.cms.governance.service.CmsEnvService;
import com.dap.dao.model.QueryResult;
import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: t_cms_env表对应的控制层类
 * @author: Fesine
 * @createTime:2018/08/20
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/08/20
 */
@RestController
@RequestMapping("/v1")
public class CmsEnvController {
    private static final Logger logger = LoggerFactory.getLogger(CmsEnvController.class);
    @Autowired
    private CmsEnvService cmsEnvService;
    @Autowired
    private CmsComponentInfoEnvService cmsComponentInfoEnvService;

    /**
     * 新增CmsEnvPo
     *
     * @param cmsEnvPo
     * @return result
     */
    @PostMapping("/cmsEnv")
    public Result addCmsEnv(CmsEnvPo cmsEnvPo) {
        try {
            cmsEnvService.save(cmsEnvPo);
            return ResultUtils.success(ResultEnum.CREATED, null);
        } catch (Exception e) {
            logger.error("新增失败", e);
            return ResultUtils.error(500, "新增失败");
        }

    }

    /**
     * 更新CmsEnvPo
     *
     * @param cmsEnvPo
     * @return result
     */
    @PutMapping("/cmsEnv")
    public Result updateCmsEnv(CmsEnvPo cmsEnvPo) {
        try {
            cmsEnvService.update(cmsEnvPo);
            //同时修改CmsComponentInfoEnvPo中的envName
            CmsComponentInfoEnvPo cmsComponentInfoEnvPo = new CmsComponentInfoEnvPo();
            cmsComponentInfoEnvPo.setEnvId(cmsEnvPo.getId());
            List<CmsComponentInfoEnvPo> cmsComponentInfoEnvPos = cmsComponentInfoEnvService.listAll(cmsComponentInfoEnvPo);
            for (CmsComponentInfoEnvPo cmInfo : cmsComponentInfoEnvPos) {
                cmInfo.setEnvName(cmsEnvPo.getEnvName());
                cmsComponentInfoEnvService.update(cmInfo);
            }
            return ResultUtils.success(ResultEnum.CREATED, null);
        } catch (Exception e) {
            logger.error("更新失败", e);
            return ResultUtils.error(500, "更新失败");
        }

    }

    /**
     * 删除CmsEnvPo
     *
     * @param id
     * @return result
     */
    @DeleteMapping("/cmsEnv/{id}")
    public Result deleteCmsEnv(@PathVariable("id") Integer id) {
        CmsEnvPo cmsEnvPo = new CmsEnvPo();
        cmsEnvPo.setId(id);
        cmsEnvService.delete(cmsEnvPo);
        //同时删除CmsComponentInfoEnvPo中的记录
        CmsComponentInfoEnvPo cmsComponentInfoEnvPo = new CmsComponentInfoEnvPo();
        cmsComponentInfoEnvPo.setEnvId(cmsEnvPo.getId());
        cmsComponentInfoEnvService.delete(cmsComponentInfoEnvPo);
        return ResultUtils.success(ResultEnum.DELETED, null);
    }

    /**
     * 查看CmsEnvPo详情
     *
     * @param id
     * @return result
     */
    @GetMapping("/cmsEnv/{id}")
    public Result getCmsEnv(@PathVariable("id") Integer id) {
        CmsEnvPo cmsEnvPo = new CmsEnvPo();
        cmsEnvPo.setId(id);
        cmsEnvPo = cmsEnvService.get(cmsEnvPo);
        return ResultUtils.success(cmsEnvPo);
    }

    /**
     * 查询CmsEnvPo列表
     *
     * @param cmsEnvPo
     * @return result
     */
    @GetMapping("/cmsEnvs")
    public Result listAll(CmsEnvPo cmsEnvPo) {
        List<CmsEnvPo> list = cmsEnvService.listAll(cmsEnvPo);
        return ResultUtils.success(list);
    }

    /**
     * 分页查询CmsEnvPo列表
     *
     * @param cmsEnvPo
     * @param pageIndex
     * @param pageSize
     * @return result
     */
    @GetMapping("/cmsEnvs/page")
    public Result listPage(CmsEnvPo cmsEnvPo, @RequestParam(value = "pageIndex",
            required = false, defaultValue = "1")
            Integer pageIndex, @RequestParam(value = "pageSize", required = false, defaultValue =
            "10") Integer pageSize) {
        //根据组件名
        if (StringUtils.isNotEmpty(cmsEnvPo.getEnvName())) {
            cmsEnvPo.setEnvName("%".concat(cmsEnvPo.getEnvName())
                    .concat("%"));
        } else {
            cmsEnvPo.setEnvName(null);
        }
        QueryResult<CmsEnvPo> result = cmsEnvService.listPage(cmsEnvPo, pageIndex, pageSize);
        long total = result.getTotalrecord();
        Map<String, Object> map = new HashMap(16);
        if (total > 0) {
            map.put("totalRecord", total);
            map.put("resultList", result.getResultlist());
        }
        return ResultUtils.success(map);
    }
}

